package com.am.mosl.cityandroid.citychange;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.am.mosl.cityandroid.R;
import com.am.mosl.cityandroid.activity.BaseActivity;
import com.am.mosl.cityandroid.bean.City;
import com.am.mosl.cityandroid.bean.CityList;
import com.am.mosl.cityandroid.constant.HttpConstant;
import com.am.mosl.cityandroid.message.CityChangeMessage;
import com.am.mosl.cityandroid.parser.JSONParser;
//import com.am.mosl.cityandroid.widget.SideBar;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by mosl on 14-7-1.
 * 城市切换
 */
public class CityChangeActivity extends BaseActivity {

    public static final String TAG="CityChangeActivity";

    private ListView mSortListView;
//    private TextView mDialog;
    private List<CityList> mCityLists;
    static FinalHttp finalHttp=new FinalHttp();
//    private SideBar mSideBar;
    private CitySortAdapter mCitySortAdapter;
    private List<City> mCityList=new ArrayList<City>();
    private List<City> mHotCitys=new ArrayList<City>();

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.city_change_layout);
        initViews();
        getHotCitys();
        getCityList();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private void initViews() {
//        mSideBar=(SideBar)findViewById(R.id.sidebar);
        mSortListView=(ListView)findViewById(R.id.country_lvcountry);
//        mDialog=(TextView)findViewById(R.id.dialog);
        mCitySortAdapter=new CitySortAdapter(this,mCityList);
        mSortListView.setAdapter(mCitySortAdapter);
//        mSideBar.setListView(mSortListView);
//        mSideBar.setTextView(mDialog);

        mSortListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView cityId = (TextView)view.findViewById(R.id.city_id);
                TextView cityName = (TextView)view.findViewById(R.id.title);
                Toast.makeText(CityChangeActivity.this,cityName.getText().toString(),Toast.LENGTH_SHORT).show();
                CityChangeMessage message = new CityChangeMessage();
                message.setCityId(Integer.parseInt(cityId.getText().toString()));
                message.setCityName(cityName.getText().toString());
                EventBus.getDefault().post(message);
                
                //SAVE CITY ID
//                AccountBean accountBean = new AccountBean();
//        		accountBean.setCity_id(cityId.getText().toString());
//        		SettingUtils.saveUserInfo(CityChangeActivity.this, accountBean);
                
                CityChangeActivity.this.finish();
            }
        });
    }

    public void onBack(View view) {
        onBackPressed();
    }


    public List<City> getmCityList(List<CityList> list){
        List<City> cities=new ArrayList<City>();
        for(int i=0;i<list.size();i++){
            CityList cityList=list.get(i);
            for(int j=0;j<cityList.getCitys().size();j++){
                City city=new City();
                city.setCity(cityList.getCitys().get(j).getCity());
                city.setInitial(cityList.getInitial());
                city.setId(cityList.getCitys().get(j).getId());
                city.setIsopen(cityList.getCitys().get(j).getIsopen());
                cities.add(city);
            }
        }
        return cities;
    }

    public void getCityList() {
        finalHttp.post(HttpConstant.CityListHttp, new AjaxCallBack<Object>() {
            @Override
            public boolean isProgress() {
                return super.isProgress();
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = (String) jsonObject.opt("status");
                    if (status.endsWith("1")) {
                        JSONArray data = (JSONArray) jsonObject.opt("data");
                        Log.d(TAG, data.toString());
                        mCityLists = JSONParser.getCityList(data.toString());
                        mCityList = getmCityList(mCityLists);
                        mCitySortAdapter.applend(mCityList);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getHotCitys(){
        finalHttp.post(HttpConstant.CityHotListHttp,new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                try {
                    JSONObject jsonObject=new JSONObject(o.toString());
                    JSONArray jsonArray = (JSONArray)jsonObject.opt("data");
                    mHotCitys = JSON.parseArray(jsonArray.toString(),City.class);
                    for(int i=0;i<mHotCitys.size();i++){
                        mHotCitys.get(i).setInitial("热门城市");
                    }
                    mCitySortAdapter.applendHotCity(mHotCitys);
                } catch (JSONException e) {
                    e.printStackTrace();
                }catch (Exception e){
                }
            }
        });
    }

}